# PLOTS, CODE

library(plyr)
library(ggplot2)
library(zoo)
library(scales)
library(stringr)
library(dplyr)
library(glue)
library(tidyverse)
library(text2vec)
library(lubridate)
library(data.table)
library(quanteda)
library(quanteda.textstats)
library(rddtools)
library(magrittr)
library(stargazer)
library(lmtest)
library(fixest)
library(modelsummary)
library(readxl)
library(cowplot)
library(texreg)
library(rdd)

# LOAD DATA
load("path to data_final.rda")

df_final <- subset(df_final, party != 1) # removing speaker
df_final <- subset(df_final, party != 0) # removing process speeches

# Plot, RDD, neoliberalism

df_final$yearmon <- format(df_final$date, "%Y-%m")
df_final$word <- str_count(df_final$tokens_edit, "markedsøkonomi|frie marked|markedsreform") #Finder strings, der indeholder word
bymonthyear_word <- aggregate(cbind(word)~yearmon, data=df_final,FUN=sum)

bymonthyear_word$yearmon <- paste(bymonthyear_word$yearmon, "01", sep="-")
bymonthyear_word$yearmon <- as.Date(bymonthyear_word$yearmon)

bymonthyear_word <- bymonthyear_word %>%
  filter(format(yearmon, '%Y') %between% c('1974', '2009'))

plot_neo_agg <- bymonthyear_word %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.2) + # Adjust point size and transparency here
  geom_smooth(method = "lm", se = TRUE) +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  labs(x = "Year and month", y = "Term Frequency") +
  theme(
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) +
  annotate("text", x = as.Date("1982-01-01"), y = max(bymonthyear_word$word, na.rm = TRUE) * 1.1, label = "Pre November 1989", size = 3, hjust = 0.5) + 
  annotate("text", x = as.Date("1996-01-01"), y = max(bymonthyear_word$word, na.rm = TRUE) * 1.1, label = "Post November 1989", size = 3, hjust = 0.5)

# Plot, RDD, discrediting socialism

df_final$yearmon <- format(df_final$date, "%Y-%m")
df_final$word <- str_count(df_final$tokens_edit, "plantyranni|planøkono|kommandoøkono") #Finder strings, der indeholder word
bymonthyear_word <- aggregate(cbind(word)~yearmon, data=df_final,FUN=sum)

bymonthyear_word$yearmon <- paste(bymonthyear_word$yearmon, "01", sep="-")
bymonthyear_word$yearmon <- as.Date(bymonthyear_word$yearmon)

bymonthyear_word <- bymonthyear_word %>%
  filter(format(yearmon, '%Y') %between% c('1974', '2009'))

plot_soc_agg <- bymonthyear_word %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.2) + # Adjust point size and transparency here
  geom_smooth(method = "lm", se = TRUE) +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  labs(x = "Year and month", y = "Term Frequency") +
  theme(
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) +
  annotate("text", x = as.Date("1982-01-01"), y = max(bymonthyear_word$word, na.rm = TRUE) * 1.1, label = "Pre November 1989", size = 3, hjust = 0.5) + 
  annotate("text", x = as.Date("1996-01-01"), y = max(bymonthyear_word$word, na.rm = TRUE) * 1.1, label = "Post November 1989", size = 3, hjust = 0.5)

# GRAPHS, PARTIES

library(dplyr)
library(zoo)
library(stringr)

# Neoliberalism. Looping through every party
for (i in 2:13) {
  party_sub <- df_final %>% filter(party == i)
  
  party_sub$word <- str_count(party_sub$tokens_edit, "markedsøkonomi|frie marked|markedsreform")
  
  bymonthyear_word <- aggregate(word ~ yearmon, data = party_sub, FUN = sum, na.rm = TRUE)
  
  bymonthyear_word$yearmon1 <- as.yearmon(bymonthyear_word$yearmon)
  bymonthyear_word$yearmon1 <- paste(bymonthyear_word$yearmon, "01", sep = "-")
  bymonthyear_word$yearmon1 <- as.Date(bymonthyear_word$yearmon1)
  
  bymonthyear_word <- na.omit(bymonthyear_word)
  assign(paste0("merged_table2_neo_", i), bymonthyear_word)
}

# Socialism. Looping through every party
for (i in 2:13) {
  party_sub <- df_final %>% filter(party == i)
  
  party_sub$word <- str_count(party_sub$tokens_edit, "plantyranni|planøkono|kommandoøkono")
  
  bymonthyear_word <- aggregate(word ~ yearmon, data = party_sub, FUN = sum, na.rm = TRUE)
  
  bymonthyear_word$yearmon1 <- as.yearmon(bymonthyear_word$yearmon)
  bymonthyear_word$yearmon1 <- paste(bymonthyear_word$yearmon, "01", sep = "-")
  bymonthyear_word$yearmon1 <- as.Date(bymonthyear_word$yearmon1)
  
  bymonthyear_word <- na.omit(bymonthyear_word)
  assign(paste0("merged_table2_soc", i), bymonthyear_word)
}

# Plots, pushers

v_neo_plot <- merged_table2_neo_4 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Liberals, \nneoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

r_neo_plot <- merged_table2_neo_5 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Social Liberals, \nneoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


fp_neo_plot <- merged_table2_neo_7 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Progress Party, \nneoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )



cd_neo_plot <- merged_table2_neo_10 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Centre Democrats, \nneoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


v_soc_plot <- merged_table2_soc4 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Liberals, \nsocialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


r_soc_plot <- merged_table2_soc5 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Social Liberals, \nsocialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


fp_soc_plot <- merged_table2_soc7 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Progress Party, \nsocialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


cd_soc_plot <- merged_table2_soc10 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Centre Democrats, \nsocialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

k_neo_plot <- merged_table2_neo_3 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Conservatives, \nneoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

k_soc_plot <- merged_table2_soc3 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2005-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Conservatives, \nsocialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


plot_grid_push <- plot_grid(v_neo_plot, v_soc_plot, r_neo_plot, r_soc_plot, 
                            fp_neo_plot, fp_soc_plot, cd_neo_plot, cd_soc_plot,
                            k_neo_plot, k_soc_plot,
                            nrow = 2, ncol = 5, align = "h", axis = "b")

# Plot, surrender

sf_neo_plot <- merged_table2_neo_6 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Socialist People's Party, neoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

sf_soc_plot <- merged_table2_soc6 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Socialist People's Party, socialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )


plot_grid_surrender <- plot_grid(sf_neo_plot, sf_soc_plot, 
                              nrow = 1, ncol = 2, align = "h", axis = "b")

# Plot, ignorers

krf_neo_plot <- merged_table2_neo_12 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Christian People's Party, neoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

krf_soc_plot <- merged_table2_soc12 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Christian People's Party, socialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

s_soc_plot <- merged_table2_soc2 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Social Democrats, socialism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

s_neo_plot <- merged_table2_neo_2 %>% 
  mutate(yearmon = as.Date(paste0(yearmon, "-01"))) %>% 
  filter(yearmon >= as.Date("1974-01-01") & yearmon <= as.Date("2009-01-01")) %>% 
  select(yearmon, word) %>% 
  mutate(D = ifelse(yearmon >= as.Date("1989-11-01"), "Post November 1989", "Pre November 1989")) %>% 
  ggplot(aes(x = yearmon, y = word, color = D)) +
  geom_point(size = 0.8, alpha = 0.5) + # Adjust point size and transparency here
  geom_smooth(method = "lm") +
  scale_color_manual(values = c("black", "black")) + # Greyscale colors
  geom_vline(xintercept = as.numeric(as.Date("1989-11-01")), linetype = "dashed", color = "black") + # Demarcation line
  theme_minimal() + # Minimal theme
  ggtitle("Social Democrats, neoliberalism") + # Add title
  labs(
    x = "Year and month", 
    y = "Term Frequency"
  ) +
  theme(
    plot.title = element_text(size = 15), # Adjust title size and make it bold
    axis.title = element_text(size = 14),  # Increase the size of the axis titles
    axis.text = element_text(size = 12),   # Increase the size of the axis text labels
    legend.position = "none", # Remove the legend
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

plot_grid_ignore <- plot_grid(krf_neo_plot, krf_soc_plot,
                              s_neo_plot, s_soc_plot,
                              nrow = 2, ncol = 2, align = "h", axis = "b")


# WORDSCORES

# Loading in Wordscores-models - combined together
setwd("FOLDER PATH")
wordscores_comb_tog <- load("wordscores_combined_together.RData")
wordscores_comb_tog <- pred_ws

# Create a dataframe for comb_tog
fit_names <- names(wordscores_comb_tog$fit)
fit_values <- unname(wordscores_comb_tog$fit)
se_fit_values <- rep(wordscores_comb_tog$se.fit, each = length(fit_values) / length(fit_names))
ws_comb_tog_df <- data.frame(Fit_Name = rep(fit_names, each = length(fit_values) / length(fit_names)),
                             Fit_Value = fit_values,
                             SE_Fit_Value = se_fit_values)
# Define the mapping of old values to new values
rename_mapping <- c("0" = "Pre fall of Berlin Wall", "1" = "Post fall of Berlin Wall")
ws_comb_tog_df$period <- factor(ws_comb_tog_df$Fit_Name, levels = names(rename_mapping), labels = rename_mapping)

# Making figure

# Calculate upper and lower bounds for confidence intervals
ws_comb_tog_df$lower_bound <- ws_comb_tog_df$Fit_Value - 1.96 * ws_comb_tog_df$SE_Fit_Value
ws_comb_tog_df$upper_bound <- ws_comb_tog_df$Fit_Value + 1.96 * ws_comb_tog_df$SE_Fit_Value

ws_comb_tog_df <- ws_comb_tog_df %>%
  filter(Fit_Name == 1 | Fit_Name == 0)

# Create the plot
wordscore_plot_comb_tog <- ggplot(ws_comb_tog_df, aes(y = period)) +
  geom_point(aes(x = Fit_Value), size = 1) +
  geom_errorbar(aes(xmin = lower_bound, xmax = upper_bound), width = 0.2) +
  labs(x = "Wordscore", y = "Period") +
  theme_minimal()+
  theme(
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

# Loading in Wordscores-model - combined by party
setwd("FOLDER PATH")
wordscores_comb_parties <- load("wordscores_parties.RData")
wordscores_comb_parties <- pred_ws

# Create a dataframe for comb_tog
fit_names <- names(wordscores_comb_parties$fit)
fit_values <- unname(wordscores_comb_parties$fit)
se_fit_values <- rep(wordscores_comb_parties$se.fit, each = length(fit_values) / length(fit_names))
ws_comb_parties_df <- data.frame(Fit_Name = rep(fit_names, each = length(fit_values) / length(fit_names)),
                                 Fit_Value = fit_values,
                                 SE_Fit_Value = se_fit_values)
ws_comb_parties_df <- ws_comb_parties_df %>%
  filter(!grepl("7|13|14|19|98|11|33", Fit_Name) & Fit_Name != "0")

# Define the mapping of old values to new values
rename_mapping <- c("2" = "Social Democrats, pre fall",
                    "2_1" = "Social Democrats, post fall",
                    "3" = "Conservatives, pre fall",
                    "3_1" = "Conservatives, post fall",
                    "4" = "Liberals, pre fall",
                    "4_1" = "Liberals, post fall",
                    "5" = "Social Liberals, pre fall",
                    "5_1" = "Social Liberals, post fall",
                    "6" = "Socialist People's Party, pre fall",
                    "6_1" = "Socialist People's Party, post fall",
                    "10" = "Centre Democrats, pre fall",
                    "10_1" = "Centre Democrats, post fall",
                    "12" = "Christian People's Party, pre fall",
                    "12_1" = "Christian People's Party, post fall")
ws_comb_parties_df$party <- factor(ws_comb_parties_df$Fit_Name, levels = names(rename_mapping), labels = rename_mapping)

# Making figure

# Calculate upper and lower bounds for confidence intervals
ws_comb_parties_df$lower_bound <- ws_comb_parties_df$Fit_Value - 1.96 * ws_comb_parties_df$SE_Fit_Value
ws_comb_parties_df$upper_bound <- ws_comb_parties_df$Fit_Value + 1.96 * ws_comb_parties_df$SE_Fit_Value

# Create the plot
wordscore_plot_comb_parties <- ggplot(ws_comb_parties_df, aes(y = party)) +
  geom_point(aes(x = Fit_Value), size = 1) +
  geom_errorbar(aes(xmin = lower_bound, xmax = upper_bound), width = 0.2) +
  labs(x = "Wordscore", y = "Party") +
  theme_minimal()+
  theme(
    panel.background = element_rect(fill = "white", color = NA),
    plot.background = element_rect(fill = "white", color = NA),
    panel.border = element_blank()
  )

